<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */
namespace Media;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
//use Zend\Authentication\Storage;
use Zend\Mail\Transport\Smtp as SmtpTransport;
use Zend\Mail\Transport\SmtpOptions;
use Zend\Session\SessionManager;
use Zend\Session\Container;
use Media\Component\AlbumComponent;
use Media\Component\PhotoComponent;
use Media\Component\VideoComponent;

class Module
{

    public function onBootstrap(MvcEvent $e)
    {
        $eventManager = $e->getApplication()->getEventManager();
        $serviceManager      = $e->getApplication()->getServiceManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
        $this->bootstrapSession($e);
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function bootstrapSession($e)
    {
    	$session = $e->getApplication()
    	->getServiceManager()
    	->get('Zend\Session\SessionManager');
    	$session->start();
    
    	$container = new Container('initialized');
    	if (!isset($container->init)) {
    		$session->regenerateId(true);
    		$container->init = 1;
    	}
    }
    
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__
                )
            )
        );
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                    'AlbumComponent' => function ($sm){
                        return new AlbumComponent($sm->get('doctrine.entitymanager.orm_default'));
                    },
                    'PhotoComponent' => function ($sm){
                        return new PhotoComponent($sm->get('doctrine.entitymanager.orm_default'));
                    },
                    'VideoComponent' => function ($sm){
                        return new VideoComponent($sm->get('doctrine.entitymanager.orm_default'));
                    },
                'mail.transport' => function ($serviceManager)
                {
                    $config = $serviceManager->get('Config');
                    $transport = new SmtpTransport();
                    $transport->setOptions(new SmtpOptions($config['mail']['transport']['options']));
                    
                    return $transport;
                },
                'Zend\Session\SessionManager' => function ($sm) {
                	$config = $sm->get('config');
                	if (isset($config['session'])) {
                		$session = $config['session'];
                
                		$sessionConfig = null;
                		if (isset($session['config'])) {
                			$class = isset($session['config']['class'])  ? $session['config']['class'] : 'Zend\Session\Config\SessionConfig';
                			$options = isset($session['config']['options']) ? $session['config']['options'] : array();
                			$sessionConfig = new $class();
                			$sessionConfig->setOptions($options);
                		}
                
                		$sessionStorage = null;
                		if (isset($session['storage'])) {
                			$class = $session['storage'];
                			$sessionStorage = new $class();
                		}
                
                		$sessionSaveHandler = null;
                		if (isset($session['save_handler'])) {
                			// class should be fetched from service manager since it will require constructor arguments
                			$sessionSaveHandler = $sm->get($session['save_handler']);
                		}
                
                		$sessionManager = new SessionManager($sessionConfig, $sessionStorage, $sessionSaveHandler);
                
                		if (isset($session['validators'])) {
                			$chain = $sessionManager->getValidatorChain();
                			foreach ($session['validators'] as $validator) {
                				$validator = new $validator();
                				$chain->attach('session.validate', array($validator, 'isValid'));
                
                			}
                		}
                	} else {
                		$sessionManager = new SessionManager();
                	}
                	Container::setDefaultManager($sessionManager);
                	return $sessionManager;
                },
    		),
    	);
    }
   
    
}
